package wens.pig.dhm.formplugin.patrolscheme;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.exception.KDBizException;
import kd.bos.form.FormShowParameter;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.Arrays;
import java.util.List;


/**
 *严密闭巡查方案
 * coyle
 * 20231020
 */
public class SchemePatrolMulListPlugin extends AbstractListPlugin {

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);
        checkOrg(getView().getFormShowParameter());
    }

    /**
     * “种猪场”、“扩繁场”、“原种场”，“区域公猪站”
     * 对应编码
     */
    private static final List<String> ORG_PATTERN = Arrays.asList("OF-28", "OF-12", "OF-40", "OF-65");

    private void checkOrg(FormShowParameter view) {
        DynamicObject orgData = BusinessDataServiceHelper.loadSingle(RequestContext.get().getOrgId(), "bos_org", "orgpattern");

        DynamicObject orgpattern = orgData.getDynamicObject("orgpattern");
        if (orgpattern != null) {
            String patternNo = orgpattern.getString("number");
            if (ORG_PATTERN.contains(patternNo)) {
                throw new KDBizException(String.format("请在温氏集团、各事业部、三级公司、四级公司维护!"));
            }
        } else {
            throw new KDBizException(String.format("当前所选“组织单元”的『形态』为空,请检查!"));
        }
    }
}
